<?php
class Maintenance extends CI_Controller {

    function __construct()
	{
		parent::__construct();
	}

    function generate_db_tables_general() {
        $this->load->helper('file');
        $newline = "\r\n";
        $tab = "\t";

        $tables = $this->db->list_tables();

        $data = '<?php'.$newline;
        $data.= '$config["tables"] = array('.$newline;

        $size_table = count($tables);

        foreach ($tables as $table_key => $table) {
            $data.= $tab.'"'.$table.'" => array('.$newline;

            $fields = $this->db->field_data($table);

            $size_fields = count($fields);

            foreach ($fields as $field_key => $field) {
                $data.= $tab.$tab.'"'.$field->name.'" => array("type" => "'.$field->type.'")';
                if ($size_fields != $field_key + 1) $data .= ',';
                $data.= $newline;
            }

            $data.=$tab.')';
            if ($size_table != $table_key + 1) $data .= ',';
            $data.= $newline;
        }
        $data.=');'.$newline;
        $data .= '?>';

        if ( ! write_file(APPPATH.'config/db_tables.php', $data)) {
            echo 'Unable to write the file';
        }
        else {
            echo 'File written!';
        }
    }

    function generate_db_tables() {
        $this->load->helper('file');
        $newline = "\r\n";
        $tab = "\t";

        $tables = $this->db->list_tables();

        $data = '<?php'.$newline;
        $data.= '$config["tables"] = array('.$newline;

        $size_table = count($tables);

        foreach ($tables as $table_key => $table) {
            $data.= $tab.'"'.$table.'" => array('.$newline;

            //$fields = $this->db->field_data($table);
            $fields = $this->db->query("SHOW COLUMNS FROM `$table`")->result();
            $size_fields = count($fields);
            foreach ($fields as $field_key => $field) {
                $field_name = $field->Field;
                $tmp = explode('(', $field->Type);
                $field_type = $tmp[0];
                $field_null = $field->Null == 'NO' ? FALSE : TRUE;
                $field_default = $field->Default;
                if ($field_default == "" AND !$field_null) {
                    $field_default = $this->get_defaults($field_type);
                }
                $data.= $tab.$tab.'"'.$field_name.'" => array("type" => "'.$field_type.'","null"=>"'.$field_null.'","default"=>"'.$field_default.'")';
                if ($size_fields != $field_key + 1) $data .= ',';
                $data.= $newline;
            }

            $data.=$tab.')';
            if ($size_table != $table_key + 1) $data .= ',';
            $data.= $newline;
        }
        $data.=');'.$newline;
        $data .= '?>';

        if ( ! write_file(APPPATH.'config/db_tables.php', $data)) {
            echo 'Unable to write the file';
        }
        else {
            echo 'File written!';
        }
    }

    function get_defaults($type = '') {
        if (empty ($type)) return '';
        switch ($type) {
            case 'int':
            case 'tinyint':
                $result = 0;
                break;
            case 'enum':
            case 'text':
            case 'varchar':
            case 'longtext':
                $result = '';
                break;
            case 'datetime':
            case 'date':
            case 'timestamp':
                $result = '0000-00-00';
                break;
            default :
                $result = '';
                break;
        }
        return $result;
    }
	
	function change_image_url(){
		$this->load->model('content_model');
		
		$contents = $this->content_model->getAll()->result();
		
		foreach ($contents as $content){
			$data = array();
			foreach ($content as $key=>$element){
				$data[$key] = preg_replace('/(\/royaljewelry\/)/','/',$element);
			}
			$this->content_model->updateExternData($data,$content->id);
		}
	}
}
?>
